import pandas as pd
import seaborn as sns
import plotly.express as px
from copy import copy
from scipy import stats
import matplotlib.pyplot as plt
import numpy as np
import plotly.figure_factory as ff
import plotly.graph_objects as go
# Read the stock data file
stocks_df = pd.read_csv("D:\Python and Machine Learning for Financial Analysis\stock.csv")
stocks_df
# Sorted the data based on Date
stocks_df = stocks_df.sort_values(by = ['Date'])
stocks_df
# Function to normalize the prices based on the initial price
def normalize(df):
x = df.copy()
for i in x.columns[1:]:
x[i] = x[i]/x[i][0]
return x
# Function to plot interactive plot
def interactive_plot(df, title):
fig = px.line(title = title)
for i in df.columns[1:]:
fig.add_scatter(x = df['Date'], y = df[i], name = i)
fig.show()
# Plotted interactive chart
interactive_plot(stocks_df, 'Prices')
# Plotted normalized interactive chart
interactive_plot(normalize(stocks_df), 'Normalized Prices')
# Function to calculate the daily returns
def daily_return(df):
df_daily_return = df.copy()
# Looped through each stock
for i in df.columns[1:]:
# Looped through each row belonging to the stock
for j in range(1, len(df)):
# Calculated the percentage of change from the previous day
df_daily_return[i][j] = ((df[i][j]- df[i][j-1])/df[i][j-1]) * 100
# set the value of first row to zero, as previous value is not available
df_daily_return[i][0] = 0
return df_daily_return
# Got the daily returns
stocks_daily_return = daily_return(stocks_df)
stocks_daily_return
stocks_daily_return.mean()
# S&P500 average daily return is 0.049%
# Amazon average daily return is 0.15%
# Google average daily return is 0.084%
# Selected any stock, let's say Apple
stocks_daily_return['AAPL']
# Selected the S&P500 (Market)
stocks_daily_return['sp500']
# plotted a scatter plot between the selected stock and the S&P500 (Market)
stocks_daily_return.plot(kind = 'scatter', x = 'sp500', y = 'AAPL')
# Fitted a polynomial between the selected stock and the S&P500 (Poly with order = 1 is a straight line)
# beta represents the slope of the line regression line (market return vs. stock return).
# Beta is a measure of the volatility or systematic risk of a security or portfolio compared to the entire market (S&P500)
# Beta is used in the CAPM and describes the relationship between systematic risk and expected return for assets
# Beta = 1.0, this indicates that its price activity is strongly correlated with the market.
# Beta < 1, indicates that the security is theoretically less volatile than the market (Ex: Utility stocks). If the stock is included, this will make the portfolio less risky compared to the same portfolio without the stock.
# Beta > 1, indicates that the security's price is more volatile than the market. For instance, Tesla stock beta is 1.26 indicating that it's 26% more volatile than the market.
# Tech stocks generally have higher betas than S&P500 but they also have excess returns
# MGM is 65% more volatile than the S&P500!
beta, alpha = np.polyfit(stocks_daily_return['sp500'], stocks_daily_return['AAPL'], 1)
print('Beta for {} stock is = {} and alpha is = {}'.format('AAPL', beta, alpha))
# Now let's plot the scatter plot and the straight line on one plot
stocks_daily_return.plot(kind = 'scatter', x = 'sp500', y = 'AAPL')
# Straight line equation with alpha and beta parameters
# Straight line equation is y = beta * rm + alpha
plt.plot(stocks_daily_return['sp500'], beta * stocks_daily_return['sp500'] + alpha, '-', color = 'r')
# Fitted a polynomial between the selected stock and the S&P500 (Poly with order = 1 is a straight line)
beta, alpha = np.polyfit(stocks_daily_return['sp500'], stocks_daily_return['TSLA'], 1)
print('Beta for {} stock is = {} and alpha is = {}'.format('TSLA', beta, alpha))
# Now let's plot the scatter plot and the straight line on one plot
stocks_daily_return.plot(kind = 'scatter', x = 'sp500', y = 'TSLA')
# Straight line equation with alpha and beta parameters
# Straight line equation is y = beta * rm + alpha
plt.plot(stocks_daily_return['sp500'], beta * stocks_daily_return['sp500'] + alpha, '-', color = 'r')
beta
# Let's calculate the average daily rate of return for S&P500
stocks_daily_return['sp500'].mean()
# Let's calculate the annualized rate of return for S&P500
# Noted that out of 365 days/year, stock exchanges are closed for 104 days during weekend days (Saturday and Sunday)
# Checked my answers with: https://dqydj.com/sp-500-return-calculator/
rm = stocks_daily_return['sp500'].mean() * 252
rm
# Assumed risk free rate is zero
# Also you can use the yield of a 10-years U.S. Government bond as a risk free rate
rf = 0
# Calculated return for any security (TSLA) using CAPM
ER_TSLA = rf + (beta * (rm-rf))
ER_TSLA
# Calculated Beta for AT&T first
beta, alpha = np.polyfit(stocks_daily_return['sp500'], stocks_daily_return['T'], 1)
print('Beta for {} stock is = {} and alpha is = {}'.format('T', beta, alpha))
# Calculated return for AT&T using CAPM
ER_T = rf + (beta * (rm - rf))
print(ER_T)
# Let's create a placeholder for all betas and alphas (empty dictionaries)
beta = {}
alpha = {}
# Looped on every stock daily return
for i in stocks_daily_return.columns:
# Ignored the date and S&P500 Columns
if i != 'Date' and i != 'sp500':
# plotted a scatter plot between each individual stock and the S&P500 (Market)
stocks_daily_return.plot(kind = 'scatter', x = 'sp500', y = i)
# Fit a polynomial between each stock and the S&P500 (Poly with order = 1 is a straight line)
b, a = np.polyfit(stocks_daily_return['sp500'], stocks_daily_return[i], 1)
plt.plot(stocks_daily_return['sp500'], b * stocks_daily_return['sp500'] + a, '-', color = 'r')
beta[i] = b
alpha[i] = a
plt.show()
# Let's view Beta for every stock
beta
# Let's view alpha for each of the stocks
# Alpha describes the strategy's ability to beat the market (S&P500)
# Alpha indicates the “excess return” or “abnormal rate of return,”
# A positive 0.175 alpha for Tesla means that the portfolio’s return exceeded the benchmark S&P500 index by 17%.
alpha
# Let's do the same plots but in an interactive way
# Explored some wierd points in the dataset: Tesla stock return was at 24% when the S&P500 return was -0.3%!
for i in stocks_daily_return.columns:
if i != 'Date' and i != 'sp500':
# Used plotly express to plot the scatter plot for every stock vs. the S&P500
fig = px.scatter(stocks_daily_return, x = 'sp500', y = i, title = i)
# Fit a straight line to the data and obtain beta and alpha
b, a = np.polyfit(stocks_daily_return['sp500'], stocks_daily_return[i], 1)
# Plotted the straight line
fig.add_scatter(x = stocks_daily_return['sp500'], y = b*stocks_daily_return['sp500'] + a)
fig.show()
# Obtained a list of all stock names
keys = list(beta.keys())
keys
# Defined the expected return dictionary
ER = {}
rf = 0 # assumed risk free rate is zero in this case
rm = stocks_daily_return['sp500'].mean() * 252 # this is the expected return of the market
rm
for i in keys:
# Calculated return for every security using CAPM
ER[i] = rf + (beta[i] * (rm-rf))
for i in keys:
print('Expected Return Based on CAPM for {} is {}%'.format(i, ER[i]))
# Assumed equal weights in the portfolio
portfolio_weights = 1/8 * np.ones(8)
portfolio_weights
# Calculated the portfolio return
ER_portfolio = sum(list(ER.values()) * portfolio_weights)
ER_portfolio
print('Expected Return Based on CAPM for the portfolio is {}%\n'.format(ER_portfolio))
ER['AMZN']
# Calculate the portfolio return
ER_portfolio_2 = 0.5 * ER['AAPL'] + 0.5 * ER['AMZN']
ER_portfolio_2